package com.heima.file.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.heima.file.config.MinIOConfig;
import com.heima.file.config.MinIOConfigProperties;
import com.heima.file.config.OssConfig;
import com.heima.file.config.OssConfigProperties;
import com.heima.file.service.FileStorageService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.springframework.util.StringUtils;
import sun.net.www.content.image.jpeg;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@EnableConfigurationProperties(OssConfigProperties.class)
@Import(OssConfig.class)
public class OssFileStorageService implements FileStorageService {

    @Autowired
    private OssConfigProperties ossConfigProperties;

    @Autowired
    private OSS ossClient;


    @Override
    public String uploadImgFile(String prefix, String filename, InputStream inputStream) {

        return uploadFile(filename, inputStream);
    }

    private String uploadFile(String filename, InputStream inputStream) {
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfigProperties.getBucketName(),
                filename, inputStream);

        // 上传字符串。
        ossClient.putObject(putObjectRequest);
        return ossConfigProperties.getUrlEndpoint() + filename;
    }

    @Override
    public String uploadHtmlFile(String prefix, String filename, InputStream inputStream) {
        return uploadFile(filename,inputStream);
    }

    @Override
    public void delete(String pathUrl) {
        pathUrl = pathUrl.replace(ossConfigProperties.getUrlEndpoint(),"");
        ossClient.deleteObject(ossConfigProperties.getBucketName(),pathUrl);
    }

    @Override
    public byte[] downLoadFile(String pathUrl) {
        pathUrl = pathUrl.replace(ossConfigProperties.getUrlEndpoint(),"");
        // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        OSSObject ossObject = ossClient.getObject(ossConfigProperties.getBucketName(), pathUrl);
        InputStream inputStream = ossObject.getObjectContent();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while (true) {
            try {
                if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            byteArrayOutputStream.write(buff, 0, rc);
        }
        try {
            ossObject.close();
        } catch (IOException e) {
            log.error("关闭oss流失败",e);
        }
        return byteArrayOutputStream.toByteArray();

    }
}
